package com.fare.taluo.application.impl.plane.mycare;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dayatang.domain.InstanceFactory;
import com.fare.taluo.GlobalsConst;
import com.fare.taluo.application.plane.mycare.UserCareLineApplication;
import com.fare.taluo.application.system.auth.UserInfoApplication;
import com.fare.taluo.core.domain.config.city.PortEntity;
import com.fare.taluo.core.domain.config.line.UserCareLineEntity;
import com.fare.taluo.exception.used.ObjectRepeatException;
import com.fare.taluo.vo.auth.UserInfoVO;
import com.fare.taluo.vo.core.UserCareLineEntityVO;

@Scope("singleton")
@Service("userCareLineApplication")
public class UserCareLineApplicationImpl implements UserCareLineApplication {
	@Override
	@Transactional(value = "txManager")
	public List<UserCareLineEntityVO> findCareLineVO(Long userId) {
		UserCareLineEntity userCareLineEntity = new UserCareLineEntity();
		List<UserCareLineEntity> entityList = userCareLineEntity.findLine(userId);
		List<UserCareLineEntityVO> voList = new ArrayList<UserCareLineEntityVO>();
		if (entityList != null&&entityList.size() > 0) {			 
				for (UserCareLineEntity entity : entityList) {
					voList.add(new UserCareLineEntityVO(entity.getId(),
							entity.getUserId(), entity.getUsername(),entity.getDepartcity(),
							entity.getArrivalcity(), entity.getCreateTime(), entity.getDepartcitycode(),
							entity.getArrivalcitycode(), entity.getIscity(), entity.getPriority()));
	              }
		}
		return voList;
  }
	@Override
	public boolean hasCareLine(Long userId) {
		List<UserCareLineEntityVO> list = this.findCareLineVO(userId);
		return list != null && !list.isEmpty();
	}
	@Override
	@Transactional(value = "txManager")
	public void save(UserCareLineEntityVO vo) {
		UserCareLineEntity entity = new UserCareLineEntity();		
		List<UserCareLineEntity> doubleEntitys = entity.findLine(vo.getUserId(), 
				vo.getDepartcitycode(), vo.getArrivalcitycode());
		if(doubleEntitys != null && !doubleEntitys.isEmpty()){
			throw new ObjectRepeatException("您设置的关注航线已存在！");
		}
		entity.setArrivalcity(vo.getArrivalcity());
		entity.setDepartcity(vo.getDepartcity());
		entity.setDepartcitycode(vo.getDepartcitycode());
		entity.setArrivalcitycode(vo.getArrivalcitycode());
		entity.setUserId(vo.getUserId());
		entity.setUsername(vo.getUsername());
		entity.setIscity(vo.getIscity());
		entity.setPriority(0);
		entity.save();
	}
	@Override
	@Transactional(value = "txManager")
	public void delete(Long id) {
		UserCareLineEntity entity = new UserCareLineEntity().get(id);		
		entity.delete();
	}
	@Override
	@Transactional(value = "txManager")
	public List<UserCareLineEntityVO> findLine(Long userId,String departcity,String arrivalcity) {
		UserCareLineEntity entity = new UserCareLineEntity();
		List<UserCareLineEntity> list = entity.findLine(userId,departcity, arrivalcity);
		return UserCareLineEntityTOUserCareLineEntityVO(list);
	}
	
	private List<UserCareLineEntityVO> UserCareLineEntityTOUserCareLineEntityVO(List<UserCareLineEntity> list){
		List<UserCareLineEntityVO> voList = new ArrayList<UserCareLineEntityVO>();
		for (UserCareLineEntity entity : list) {
			voList.add(new UserCareLineEntityVO(entity.getId(),
					entity.getUserId(), entity.getUsername(),entity.getDepartcity(),
					entity.getArrivalcity(), entity.getCreateTime(), entity.getDepartcitycode(),
					entity.getArrivalcitycode(), entity.getIscity(),entity.getPriority()));
		}
		return voList;
	}


	@Override
	@Transactional(value = "txManager")
	public void saveLogon(String lines, String username) {
		if(StringUtils.isNotBlank(lines) && StringUtils.isNotBlank(username)){
			UserCareLineEntity entity = null;
			PortEntity port = new PortEntity();
			PortEntity fe = null;
			PortEntity te = null;
			UserInfoVO user = InstanceFactory.getInstance(UserInfoApplication.class).getUserInfoByLogonid(username);
			String[] lineCodes = lines.split(GlobalsConst.SEPARATOR_COMMA);
			int priority = 0;
			for(String s : lineCodes){
				String[] cs = s.split(GlobalsConst.SEPARATOR_LINE);
				fe = port.findByPortCode(cs[0]);
				te = port.findByPortCode(cs[1]);
				entity = new UserCareLineEntity();
				entity.setArrivalcity(te.getCityName());
				entity.setArrivalcitycode(te.getCode());
				entity.setDepartcity(fe.getCityName());
				entity.setDepartcitycode(fe.getCode());
				entity.setUserId(user.getUserId());
				entity.setUsername(username);
				entity.setPriority(priority++);
				entity.save();
			}
		}
		
	}
}